from django.db import models

class SupplierJob(models.Model):
	'''Represents a job titles'''
	name = models.CharField(maxlength=100)
	
	class Admin:
		pass
	
	def __unicode__(self):
		return self.name

class CustomerJob(models.Model):
	'''Represents a job titles'''
	name = models.CharField(maxlength=100)
	
	class Admin:
		pass
	
	def __unicode__(self):
		return self.name

class Customer(models.Model):
	'''Represents a data for customers required for reports'''
	long_name = models.CharField(maxlength=300)
	short_name = models.CharField(maxlength=30)
	rnn = models.CharField(maxlength=12)
	
	class Admin:
		pass
	
	def __unicode__(self):
		return self.short_name
	
class Supplier(models.Model):
	'''Represents a data for suppliers required for reports'''
	long_name = models.CharField(maxlength=300)
	short_name = models.CharField(maxlength=30)
	rnn = models.CharField(maxlength=12)
	
	class Admin:
		pass
	
	def __unicode__(self):
		return self.short_name

class SupplierWorker(models.Model):
	'''Represents supplier workers'''
	firstname = models.CharField(maxlength=30)
	lastname = models.CharField(maxlength=30)
	middlename = models.CharField(maxlength=30)
	supplier = models.ForeignKey(Supplier)
		
	def __unicode__(self):
		return u'%s %s %s' % (self.lastname, self.firstname, self.middlename)

class CustomerWorker(models.Model):
	'''Represents customer workers'''
	firstname = models.CharField(maxlength=30)
	lastname = models.CharField(maxlength=30)
	middlename = models.CharField(maxlength=30)
	customer = models.ForeignKey(Customer)
	
	def __unicode__(self):
		return u'%s %s %s' % (self.lastname, self.firstname, self.middlename)


class LocomotiveModel(models.Model):
	"""Represents a locomotive model. This is small list which is rarely
	changed. So it's a subject of discussion to convert it to a choice tuple 
	"""
	name = models.CharField(maxlength=30)
	
	class Admin:
		pass
	
	def __unicode__(self):
		return self.name
	
class ChangeType(models.Model):
	'''Represents types of change, actually containing of 2 possible values
	1) night
	2) day
	It is subject of discussion to make it choice tuple (or even a boolean field?)
	'''
	name = models.CharField(maxlength=30)
	
	class Admin:
		pass
	
	def __unicode__(self):
		return self.name
	
class Locomotive(models.Model):
	'''Represents a locomotive'''
	model = models.ForeignKey(LocomotiveModel)
	loc_number = models.CharField(maxlength=10)
	section_count = models.IntegerField()
	customer = models.ForeignKey(Customer)
	
	class Admin:
		pass
	
	def __unicode__(self):
		return u'%s %s' % (self.model.name, self.loc_number)

class LocJournal(models.Model):
	'''Represents a journal of a locomotive, where all notes a written
	by locomotive drivers
	'''
	log_date = models.DateField()
	log_record = models.CharField(maxlength=300)
	locomotive = models.ForeignKey(Locomotive)
	
	def __unicode__(self):
		return u'[%s %s]: %s' % (unicode(self.locomotive), self.log_date, self.log_record)

class MaterialType(models.Model):
	'''Represents material types'''
	name = models.CharField(maxlength=30)
	
	class Admin:
		pass
	
	def __unicode__(self):
		return self.name

class MeasurementUnit(models.Model):
	'''Represents all measurement units (and of course it is static)'''
	name = models.CharField(maxlength=10)
	
	class Admin:
		pass
	
	def __unicode__(self):
		return self.name

class Material(models.Model):
	'''Represents materials. This model, of course does not have deal with
	the amounts of materials, it only designates their names and measurement units
	they are measured in
	'''
	nomenclature_number = models.CharField(maxlength=7,primary_key=True)
	material_type = models.ForeignKey(MaterialType)
	measurement_unit = models.ForeignKey(MeasurementUnit)
	name = models.CharField(maxlength=100)
	
	class Admin:
		pass
	
	def __unicode__(self):
		return self.name

class Change(models.Model):
	'''Represents change (date and "daily" or "nightly")'''
	change_type = models.ForeignKey(ChangeType)
	date_time_begin = models.DateTimeField()
	date_time_end = models.DateTimeField()
	
	class Admin:
		pass
	
	def __unicode__(self):
		return u"%s (%s)" % (self.date, unicode(self.change_type))

class ServiceType(models.Model):
	'''Represents a service type'''
	name = models.CharField(maxlength=100)
	
	class Admin:
		pass
	
	def __unicode__(self):
		return self.name

class Area(models.Model):
	'''Represents area where the service occurs'''
	name = models.CharField(maxlength=30)
	
	class Admin:
		pass
	
	def __unicode__(self):
		return self.name

class LocomotiveEntry(models.Model):
	'''Represents a locomotive entry'''
	locomotive = models.ForeignKey(Locomotive)
	area = models.ForeignKey(Area)
	change = models.ForeignKey(Change)
	driver = models.ForeignKey(CustomerWorker)
	path_number = models.CharField(maxlength=10) # don't know how many 
							# digits does it contain
	
	def __unicode__(self):
		return u'%s %s %s' % (unicode(self.area), unicode(self.change), unicode(self.locomotive))

class Service(models.Model):
	'''Represents a services that are made during a locomotive entry'''
	entry = models.ForeignKey(LocomotiveEntry)
	service_type = models.ForeignKey(ServiceType)
	
	def __unicode__(self):
		return u'%s %s' % (unicode(self.entry), unicode(self.service_type))

class Work(models.Model):
	'''Represents work done during a service on a locomotive'''
	service = models.ForeignKey(Service)
	description = models.CharField(maxlength=200)
	
	def __unicode__(self):
		return self.description

class MaterialConsumption(models.Model):
	'''Represents amount of material consumption to make a specified work'''
	work = models.ForeignKey(Work)
	material = models.ForeignKey(Material)
	amount = models.FloatField() #max_digits=12,decimal_places=3, core=True) # 10^9 is enough
	
	def __unicode__(self):
		return u'%s %s %s' % (self.material.name, unicode(self.material.measurement_unit),
				self.amount)
		# here we might want to change format specificator

class ChangeSupplierWorkers(models.Model):
	'''Represents workers belonging to a change'''
	change = models.ForeignKey(Change)
	worker = models.ForeignKey(SupplierWorker)
	
	def __unicode__(self):
		return u'%s %s' % (unicode(self.change), unicode(self.worker))

class ChangeCustomerWorkers(models.Model):
	'''Represents workers belonging to a change'''
	change = models.ForeignKey(Change)
	worker = models.ForeignKey(CustomerWorker)
	
	def __unicode__(self):
		return u'%s %s' % (unicode(self.change), unicode(self.worker))


class Fix(models.Model):
	'''Represents works which were done to fix records in a log'''
	journal_record = models.ForeignKey(LocJournal, core=True)
	work = models.ForeignKey(Work)
	
	def __unicode__(self):
		return u'%s - %s' % (self.journal_record.log_record, unicode(self.work))

